﻿using System;
using System.Collections.Generic;
using System.Text;
using Tools;
using System.Windows.Forms;

namespace ZKInstall
{
    public class NotifyIconHelper
    {
        private static NotifyIconHelper instance = null;
        public static NotifyIcon NotifyIcon = new NotifyIcon();

        #region 单例

        private NotifyIconHelper()
        {
            SetupNotifyIcon();
        }
        public static NotifyIconHelper Instance()
        {
            if (instance == null)
            {
                instance = new NotifyIconHelper();
            }
            return instance;
        }
        #endregion

        #region 提示信息

        /// <summary>
        /// 提示信息
        /// </summary>
        /// <param name="title"></param>
        /// <param name="text"></param>
        public void ShowBalloonTip(string title, string text)
        {
            if (String.IsNullOrEmpty(text))
                return;
            try
            {
                NotifyIcon.BalloonTipTitle = title;
                NotifyIcon.BalloonTipText = text;
                NotifyIcon.BalloonTipIcon = ToolTipIcon.None;

                NotifyIcon.ShowBalloonTip(5000);
            }
            catch (Exception ex)
            {
                LogHelper.SaveErrorLog("ShowBalloonTip BalloonTipTitle:[" + title + "]\r\nBalloonTipText:[" + text + "]", ex);
            }
        }
        #endregion

        #region 托盘菜单

        /// <summary>
        /// 托盘菜单
        /// </summary>
        private void SetupNotifyIcon()
        {
            //NotifyIcon.Icon = Properties.Resources.logo;
            NotifyIcon.ContextMenuStrip = new ContextMenuStrip();

            NotifyIcon.ContextMenuStrip.Items.AddRange(new ToolStripItem[] {
                                     new ToolStripSeparator(),
                                     new ToolStripMenuItem("退出", ZKIDRTool.Properties.Resources.Exit, new EventHandler(ExitMenuItem_Click)) 
                            });
            NotifyIcon.Visible = true;
        }
        #endregion

        // 退出
        private void ExitMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要退出托盘应用程序吗？", "提示！", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                Application.Exit();    
                NotifyIconHelper.Instance().Hide();
            }
        }
        // 隐藏
        public void Hide()
        {
            NotifyIcon.Visible = false;
        }
    }
}
